class LoomsController < ApplicationController
  
  before_filter :authorize
  
  layout :determine_layout
  
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }
  
  def load_loom
    @loom = Loom.find(params[:id])
    @loom.loom_state = LoomState.find_by_code(1)
    @loom.doffer = User.find(session[:user_id])
    if @loom.update_attributes(params[:loom])
      flash[:notice] = 'Loom was successfully updated.'
      redirect_to :action => 'show', :id => @loom
    else
      render :action => 'edit'
    end
  end
  
  def select_fabric
    @loom = Loom.find(params[:id])
  end
  
  def clear_fabric
    @loom = Loom.find(params[:id])
    @loom.fabric = nil
    if @loom.save
      flash[:notice] = 'Fabric was successfully cleared.'
      redirect_to :action => 'show', :id => @loom
    else
      flash[:notice] = 'An error occured while saving.'
      redirect_to :action => 'show', :id => @loom
    end
  end
  
  def set_fabric
     @loom = Loom.find(params[:id])
     temp_loom = Loom.new(params[:loom])
     @loom.fabric = temp_loom.fabric
     @loom.doffer = User.find(session[:user_id])
     if @loom.save
       flash[:notice] = 'Fabric was successfully selected.'
       redirect_to :action => 'show', :id => @loom
     else
       flash[:notice] = 'An error occured while saving.'
       redirect_to :action => 'select_fabric', :id => @loom
     end
   end
  
  def add_container
    @loom = Loom.find(params[:id])
    @loom_load = LoomLoad.new
    @type_code = params[:type_code]
    @loom_load.load_type = LoadType.find_by_code(@type_code)
  end
  
  def load_container
    @loom = Loom.find(params[:id])
    @loom.doffer = User.find(session[:user_id])
    @loom_load = LoomLoad.new(params[:loom_load])
    @loom_load.load_type = LoadType.find_by_code(params[:type_code])
    if @loom.load_container(@loom_load.container, @loom_load.load_type)
      if @loom.save
        flash[:notice] = 'Container was successfully added.'
      else
        flash[:notice] = 'An error occured while saving.'
      end
    else
      flash[:notice] = 'An error occured while adding.'
    end
    redirect_to :action => 'show', :id => @loom
  end
  
  # this method is only to be used when a container must be
  # removed from the loom. It does not remove disassociate
  # the container from a roll.
  def unload_container 
    @loom = Loom.find(params[:id])
    @type_code = params[:type_code].to_i
    # change this to pass an int to the method
    if @loom.unload_container(@type_code)
      if @loom.save
        flash[:notice] = "Container was successfully unloaded from the loom."
      else
        flash[:notice] = "An error occued while saveing."
      end
    else
      flash[:notice] = "An error occued while saveing. Code = " + @type_code
    end
    redirect_to :action => 'show', :id => @loom
  end
  
  # This method clears a consumed container of yarn from the loom
   
  def clear_container 
    @loom = Loom.find(params[:id])
    @type_code = params[:type_code].to_i
    
    if @loom.clear_container(@type_code)
      if @loom.save
        flash[:notice] = "Container was successfully cleared from the loom."
      else
        flash[:notice] = "An error occued while saveing."
      end
    else
      flash[:notice] = "An error occued while saveing. Code = " + @type_code
    end
    redirect_to :action => 'show', :id => @loom
  end
  
  def start_loom
    @loom = Loom.find(params[:id])
    @loom.doffer = User.find(session[:user_id])
    if @loom.start
      if @loom.save
        flash[:notice] = 'Loom is weaving.'
      else
        flash[:notice] = 'An error occured while saving.'
      end
    else
      flash[:notice] = 'Loom did not start.'
    end
    redirect_to :action => 'show', :id => @loom
  end
  
  def complete_weaving
    @loom = Loom.find(params[:id])
    if @loom.complete_weaving
      if @loom.save
        flash[:notice] = 'Loom has completed the weaving process'
      else
        flash[:notice] = 'An error has occured while saving.'
      end
    else
      flash[:notice] = 'Weaving did not complete.'
    end
    redirect_to :action => 'show', :id => @loom
  end
  
  # here are the scaffold methods
  def list
    @loom_pages, @looms = paginate :looms, :order => 'number', :per_page => 10
  end

  def show
    @loom = Loom.find(params[:id])
  end

  def new
    @loom = Loom.new
  end

  def create
    @loom = Loom.new(params[:loom])
    if @loom.save
      flash[:notice] = 'Loom was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @loom = Loom.find(params[:id])
  end

  def update
    @loom = Loom.find(params[:id])
    @old_loom = @loom
    if @loom.update_attributes(params[:loom])
      flash[:notice] = 'Loom was successfully updated.'
      redirect_to :action => 'show', :id => @loom
    else
      render :action => 'edit'
    end
  end
  

  def destroy
    Loom.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
end
